package com.os.sys.service;

import com.os.sys.common.utility.PubClass;
import com.os.sys.common.utility.XJsonInfo;
import com.os.sys.model.ILoginUser;
import com.os.sys.model.T_SYS_Log;
import com.os.sys.repository.LogRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.*;

/**
 * Created by Administrator on 2017/5/27.
 */
@Service
public class LogService {
    @Resource
    private PubClass pubClass;
    @Resource
    private LogRepository logRepository;
    @Resource
    private UserService userService;
    @Value("my.tomcatLogPath")
    private String tomcatLogPath;

    @Transactional
    public void add(){
        T_SYS_Log info=new T_SYS_Log();
        info.setUserId("11");
        logRepository.save(info);
    }

    // 新增操作日志 logType不是error，info这种意思，是指日志所属模块分类
    public XJsonInfo addLog(String logType, String content) {
        ILoginUser userInfo = pubClass.getLoginUser();
        if (userInfo == null) {
            return new XJsonInfo(false, "无法获取登录用户");
        }
        T_SYS_Log info = new T_SYS_Log();
        info.setUserId(String.valueOf(userInfo.getLoginUserID()));
        info.setUserName(userInfo.getLoginUserName());
        info.setLogType(logType);
        info.setContent(content);

        logRepository.save(info);
        return new XJsonInfo();

    }

    public void showTomcatLog() throws IOException {
        String srcFilename = tomcatLogPath;
        String charset = "GBK";
        File file = new File(srcFilename);
        InputStream fileInputStream = new FileInputStream(srcFilename);
        fileInputStream.skip(file.getTotalSpace() - 200); // skip n bytes if needed, filesystem may position to the offset directly but not read really.
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream, charset));
        String singleLine;
        long fileSize = file.length();
        while (true) {
            try {
                if ((singleLine = bufferedReader.readLine()) != null) {
                    System.out.println(singleLine);
                    fileSize = Math.max(file.length(), fileSize);
                    continue;
                }
            } catch (IOException e) { // 文件被清空的时候FileInputStream会被close
                bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(srcFilename), charset));
                fileSize = file.length();
            }


            try {
                if (file.length() < fileSize) { // 文件被清空了
                    bufferedReader.close();
                    bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(srcFilename), charset));
                    fileSize = file.length();
                }
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                bufferedReader.close();
                break;
            }
        }
    }
}
